package cn.wildfire.chat.app.extendsion.ui;

import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

/**
 * Create by HEKE, 2019/9/3 10:30
 * TODO 消息弹窗
 */
public class TipDialog {

    private Context context;
    protected MaterialDialog dialog;
    private String content;
    private String title;

    public TipDialog(Context context) {
        this.context = context;
        init();
    }

    public TipDialog(Context context, String content) {
        this.context = context;
        this.content = content;
        init();
    }

    public TipDialog(Context context, String content, String title) {
        this.context = context;
        this.content = content;
        this.title = title;
        init();
    }

    private void init() {
        dialog = new MaterialDialog.Builder(context)
                .title("" + title)
                .content(content)
                .progress(true, 100)
                .cancelable(false)
                .build();
    }

    public String getContent() {
        return content;
    }

    public void setTitle(String title) {
        this.title = title;
        dialog.setTitle(this.title);
    }

    public void setContent(String content) {
        this.content = content;
        if (this.dialog != null) {
            this.dialog.setContent("" + this.content);
        }
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    /**
     * 常用提示窗口
     *
     * @param context                必须为继承自WfcBaseActivity对象的Activity上下文
     * @param title                  提示标题
     * @param message                提示内容
     * @param onMsgTipDialogListener 回调选择接口
     */
    public static void msgTip(Context context, String title, String message, OnMsgTipDialogListener onMsgTipDialogListener) {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setMessage(message);
            builder.setPositiveButton(context.getString(R.string.confirm), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (onMsgTipDialogListener != null) {
                        onMsgTipDialogListener.then(true, false);
                    }
                }
            });
            builder.setNeutralButton(context.getString(R.string.cancle_but), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (onMsgTipDialogListener != null) {
                        onMsgTipDialogListener.then(false, false);
                    }
                }
            });
            builder.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 自定义窗口弹出，默认只允许弹出一个
     */
    protected static CustomAppUIDialog customAppUIDialog;
    protected static boolean allowMoreCustomAppUIDialogShow;

    /**
     * 设置是否可以同时显示多个自定义窗口
     *
     * @param allowMoreCustomAppUIDialogShow
     */
    public static void setAllowMoreCustomAppUIDialogShow(boolean allowMoreCustomAppUIDialogShow) {
        TipDialog.allowMoreCustomAppUIDialogShow = allowMoreCustomAppUIDialogShow;
    }

    /**
     * 显示自定义样式提示窗口
     *
     * @param context                Activity上下文
     * @param title                  窗口标题
     * @param message                窗口内容
     * @param buttonTitles           按钮标题
     * @param type                   自定义窗口类型 CustomAppUIDialog type
     * @param canOutsideTouch        是否允许外部点击操作
     * @param titleset               设置标题或文本显示样式,-1 默认，0 左居中，1标题左居中，2全局中
     * @param showCover              是否显示遮盖层
     * @param onMsgTipDialogListener 回调操作选择
     */
    public static CustomAppUIDialog showCustomDialog(Context context, String title, String message, String[] buttonTitles,
                                                     int type, boolean canOutsideTouch, int titleset, boolean showCover,
                                                     final OnMsgTipDialogListener onMsgTipDialogListener) {
        try {
            if (!allowMoreCustomAppUIDialogShow) {
                if (customAppUIDialog != null && customAppUIDialog.isShowing()) {
                    return null;
                }
            }
            customAppUIDialog = new CustomAppUIDialog(context, title, message, buttonTitles, type, new CustomAppUIDialog.OnAppUIDialogCheckedListener() {
                @Override
                public void behavior(boolean confirm, boolean outtouch) {
                    if (onMsgTipDialogListener != null) {
                        onMsgTipDialogListener.then(confirm, outtouch);
                    }
                    customAppUIDialog = null;
                }
            });
            customAppUIDialog.setCanceledOnTouchOutside(canOutsideTouch);
            customAppUIDialog.show();
            switch (titleset) {
                case 0:
                    customAppUIDialog.showLeftTitleMessageCenterStyle();
                    break;
                case 1:
                    customAppUIDialog.showLeftTitle();
                    break;
                case 2:
                    customAppUIDialog.showTextAllCenter();
                    break;
                default:
                    break;
            }
            if (showCover) {
                customAppUIDialog.showCover();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customAppUIDialog;
    }

    /**
     * 显示自定义样式提示窗口
     *
     * @param context                Activity上下文
     * @param title                  窗口标题
     * @param message                窗口内容
     * @param buttonTitles           按钮标题
     * @param type                   自定义窗口类型 CustomAppUIDialog type
     * @param canOutsideTouch        是否允许外部点击操作
     * @param titleset               设置标题或文本显示样式,-1 默认，0 左居中，1标题左居中，2全局中
     * @param showCover              是否显示遮盖层
     * @param onMsgTipDialogListener 回调操作选择
     */
    public static CustomAppUIDialog showCustomDialog(Context context, int title, int message, int[] buttonTitles,
                                                     int type, boolean canOutsideTouch, int titleset, boolean showCover,
                                                     final OnMsgTipDialogListener onMsgTipDialogListener) {
        CustomAppUIDialog customAppUIDialog = null;
        try {
            customAppUIDialog = showCustomDialog(context, context.getString(title), context.getString(message),
                    new String[]{context.getString(buttonTitles[0]), context.getString(buttonTitles[1])},
                    type, canOutsideTouch, titleset, showCover, onMsgTipDialogListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customAppUIDialog;
    }

    /**
     * 显示自定义样式提示窗口
     *
     * @param context                Activity上下文
     * @param title                  窗口标题
     * @param message                窗口内容
     * @param buttonTitles           按钮标题
     * @param type                   自定义窗口类型 CustomAppUIDialog type
     * @param canOutsideTouch        是否允许外部点击操作
     * @param titleset               设置标题或文本显示样式,-1 默认，0 左居中，1标题左居中，2全局中
     * @param showCover              是否显示遮盖层
     * @param showClose              显示右上角关闭按钮
     * @param confirmStyle           确定按钮风格
     * @param cancleStyle            取消按钮风格
     * @param confirmTextColor       确定按钮字体颜色
     * @param cancleTextColor        取消按钮字体颜色
     * @param ffp                    确定与取消按钮是否占满父控件显示
     * @param onMsgTipDialogListener 回调操作选择
     */
    public static CustomAppUIDialog showCustomDialogWithNewAttrs(Context context, int title, int message, int[] buttonTitles,
                                                                 int type, boolean canOutsideTouch, int titleset, boolean showCover,
                                                                 boolean showClose, int confirmStyle, int cancleStyle, int confirmTextColor,
                                                                 int cancleTextColor, boolean ffp, final OnMsgTipDialogListener onMsgTipDialogListener) {
        CustomAppUIDialog customAppUIDialog = null;
        try {
            customAppUIDialog = showCustomDialog(context, context.getString(title), context.getString(message),
                    new String[]{context.getString(buttonTitles[0]), context.getString(buttonTitles[1])},
                    type, canOutsideTouch, titleset, showCover, onMsgTipDialogListener);
            if (showClose) {
                customAppUIDialog.showCloseButton();
            }
            customAppUIDialog.setConfirmStyle(confirmStyle, ffp);
            customAppUIDialog.setCancleStyle(cancleStyle, ffp);
            customAppUIDialog.setConfirmTextColor(confirmTextColor);
            customAppUIDialog.setCancleTextColor(cancleTextColor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customAppUIDialog;
    }

    /**
     * 显示自定义样式提示窗口
     *
     * @param context                Activity上下文
     * @param title                  窗口标题
     * @param message                窗口内容
     * @param buttonTitles           按钮标题
     * @param type                   自定义窗口类型 CustomAppUIDialog type
     * @param canOutsideTouch        是否允许外部点击操作
     * @param titleset               设置标题或文本显示样式,-1 默认，0 左居中，1标题左居中，2全局中
     * @param showCover              是否显示遮盖层
     * @param showClose              显示右上角关闭按钮
     * @param confirmStyle           确定按钮风格
     * @param cancleStyle            取消按钮风格
     * @param confirmTextColor       确定按钮字体颜色
     * @param cancleTextColor        取消按钮字体颜色
     * @param ffp                    确定与取消按钮是否占满父控件显示
     * @param onMsgTipDialogListener 回调操作选择
     */
    public static CustomAppUIDialog showCustomDialogWithNewAttrs(Context context, String title, String message, String[] buttonTitles,
                                                                 int type, boolean canOutsideTouch, int titleset, boolean showCover,
                                                                 boolean showClose, int confirmStyle, int cancleStyle, int confirmTextColor,
                                                                 int cancleTextColor, boolean ffp, final OnMsgTipDialogListener onMsgTipDialogListener) {
        CustomAppUIDialog customAppUIDialog = null;
        try {
            customAppUIDialog = showCustomDialog(context, title, message,
                    new String[]{buttonTitles[0], buttonTitles[1]}, type,
                    canOutsideTouch, titleset, showCover, onMsgTipDialogListener);
            if (showClose) {
                customAppUIDialog.showCloseButton();
            }
            customAppUIDialog.setConfirmStyle(confirmStyle, ffp);
            customAppUIDialog.setCancleStyle(cancleStyle, ffp);
            customAppUIDialog.setConfirmTextColor(confirmTextColor);
            customAppUIDialog.setCancleTextColor(cancleTextColor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customAppUIDialog;
    }

    /**
     * Create by HEKE, 2019/9/5 15:14
     * TODO 常用消息提醒窗口选择回调接口
     */
    public interface OnMsgTipDialogListener {
        void then(boolean confirm, boolean outtouch);
    }

}
